package com.example.test.operate.service.impl;

import com.example.test.operate.POJO.PO.OperateRolePO;
import com.example.test.operate.dao.IOperateFunctionDao;
import com.example.test.operate.dao.IOperateRoleDao;
import com.example.test.operate.service.IOperateRoleService;
import com.example.test.operate.util.DataStatusEnum;
import com.example.test.operate.util.LogicIdUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *
 * </desc>
 *
 * @createDate 2021-07-16
 */
@Service
public class OperateRoleServiceImpl implements IOperateRoleService {

    @Autowired
    IOperateRoleDao iOperateRoleDao;

    @Autowired
    IOperateFunctionDao iOperateFunctionDao;
/**
 * <desc>
 *        获取角色列表
 * </desc>
 *
 * @param
 * @return
 * @author liuyu
 * @createDate
 */
    @Override
    public List<OperateRolePO> getRoleList( OperateRolePO operateRolePO) {
        return iOperateRoleDao.getRoleList(operateRolePO);
    }


    /**
     * <desc>
     *        添加角色
     * </desc>
     *
     * @param
     * @return
     * @author liuyu
     * @createDate
     */
    @Override
    public Integer save(OperateRolePO operateRolePO) {
        operateRolePO.setRoleId(LogicIdUtil.bussinessId());
        operateRolePO.setSyncType(1);
        operateRolePO.setCreateTime(new Date());
        return iOperateRoleDao.save(operateRolePO);
    }

    /**
     * <desc>
     *        更新角色
     * </desc>
     *
     * @param operateRolePO
     * @return
     * @author liuyu
     * @createDate
     */
    @Override
    public Integer update(OperateRolePO operateRolePO) {
        operateRolePO.setUpdateTime(new Date());
        operateRolePO.setSyncType(2);
        return iOperateRoleDao.update(operateRolePO);
    }

    /**
     * <desc>
     *    保存用户角色
     * </desc>
     *
     * @param roleId
     * @param userId
     * @return
     * @author liuyu
     * @createDate 2021-07-21
     */
    @Override
    public Integer saveRoleUsers(@Param("roleId") String roleId, @Param("userId") String userId) {
        //拆分userId
        int strLen;
        String[] allotUser;
        if (userId != null && (strLen = userId.length()) != 0) {

            allotUser = userId.substring(0, strLen - 1).split(",");
        } else {
            allotUser = new String[]{};
        }

        //防止绕过前端提交，工程人员不可编辑
        if (roleId != null && !roleId.equals("1")) {
            //先将用户的角色删除 物理删除
            iOperateRoleDao.deleteRoleUsers(roleId,allotUser);
            //为用户分配角色
            if (allotUser.length==0) {
                return 1;
            }
        }


            Map<String, Object> params = new HashMap<>();
            params.put("allotUser", allotUser);
            params.put("roleId", roleId);
            params.put("status", DataStatusEnum.NORMAL.getType());
            params.put("createTime", new Date());
            params.put("userId", userId);
            params.put("type", 2);
            for(String s:allotUser) {
                System.out.println(s);
            }
        System.out.println(roleId);
            return iOperateRoleDao.saveRoleUsers(params);
        }

    /**
     * <desc>
     * 保存角色菜单资源
     * </desc>
     *
     * @param roleId      角色id
     * @param functionIds 资源id 多个,号分隔
     * @return
     * @author
     */
    @Override
    public Integer saveRoleFunction(String roleId, List<String> functionIds) {
        //删除之前的角色菜单 物理删除
        iOperateFunctionDao.deleteRoleFunctions(roleId);
        //拆分functionIds
//        String[] role_functionIds;
//        int strlen;
//        if(functionIds != null && (strlen=functionIds.length())!= 0){
//            role_functionIds = functionIds.substring(0,strlen-1).split(",");
//        }else {
//            role_functionIds = new String[]{};
//        }

        Map<String, Object> param = new HashMap<>();
        param.put("roleId", roleId);
        param.put("functionIds", functionIds);
        param.put("createTime", new Date());
        param.put("status", DataStatusEnum.NORMAL.getType());
        if(functionIds.isEmpty()) {
            return 1;
        }
        else {
            return iOperateRoleDao.saveRoleFunction(param);
        }
    }


}

